using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace buildingLib
{
    [Serializable()]
    public class Pet : BusinessBase<Pet>
    {
        #region Business Methods
        int m_id;
        Guid m_doorid;
        string m_type = string.Empty;
        string m_color = string.Empty;
        string m_cerid = string.Empty;
        string m_cerperiod = string.Empty;
        string m_memo = string.Empty;
        string m_kind = string.Empty;


        public int ID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_id;
            }
        }

        public Guid Doorid
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_doorid;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_doorid != value)
                {
                    m_doorid = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Type
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_type;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_type != value)
                {
                    m_type = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Color
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_color;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_color != value)
                {
                    m_color = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Cerid
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_cerid;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_cerid != value)
                {
                    m_cerid = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Cerperiod
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_cerperiod;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_cerperiod != value)
                {
                    m_cerperiod = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Memo
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_memo;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_memo != value)
                {
                    m_memo = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Kind
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_kind;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_kind != value)
                {
                    m_kind = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return m_id;
        }
        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(null, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("", "");
        }

        #endregion

        #region Factory Methods

        public static Pet NewPet(Guid doorid)
        {
            // TODO: change to use new keyword if not loading defaults
            return new Pet(doorid);
        }

        internal static Pet GetPet(SafeDataReader dr)
        {
            return new Pet(dr);
        }

        private Pet(Guid doorid)
        {
            m_doorid = doorid;
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        private Pet(SafeDataReader dr)
        {
            MarkAsChild();
            Fetch(dr);
        }

        #endregion

        #region Data Access

        private void Fetch(SafeDataReader dr)
        {
            m_id = dr.GetInt32("id");            
            m_type = dr.GetString("type");
            m_color = dr.GetString("color");
            m_cerid = dr.GetString("cerid");
            m_cerperiod = dr.GetString("cerperiod");
            m_memo = dr.GetString("memo");
            m_kind = dr.GetString("kind");
            MarkOld();
        }

        internal void Insert()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AInsertPet");
            db.AddOutParameter(dbcom, "id", DbType.Int32, m_id);
            db.AddInParameter(dbcom, "doorid", DbType.Guid, m_doorid);
            DoInsertUpdate(dbcom);
            m_id = (Int32)db.GetParameterValue(dbcom, "id");
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AUpdatePet");
            db.AddInParameter(dbcom, "id", DbType.Int32, m_id);

            DoInsertUpdate(dbcom);
            MarkOld();
        }

        private void DoInsertUpdate(DbCommand dbcom)
        {
            Database db = SQLDatabase.GetDatabase();
            
            db.AddInParameter(dbcom, "type", DbType.String, m_type);
            db.AddInParameter(dbcom, "color", DbType.String, m_color);
            db.AddInParameter(dbcom, "cerid", DbType.String, m_cerid);
            db.AddInParameter(dbcom, "cerperiod", DbType.String, m_cerperiod);
            db.AddInParameter(dbcom, "memo", DbType.String, m_memo);
            db.AddInParameter(dbcom, "kind", DbType.String, m_kind);

            db.ExecuteNonQuery(dbcom);
        }

        internal void DeleteSelf()
        {
            // if we're not dirty then don't update the database
            if (!this.IsDirty) return;

            // if we're new then don't update the database
            if (this.IsNew) return;

            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetSqlStringCommand("delete from pet where id=@id");
            db.AddInParameter(dbcom, "id", DbType.Int32, m_id);

            db.ExecuteNonQuery(dbcom);

            MarkNew();
        }

        #endregion
    }//end of class	
}//end of namespace 

